package org.bing.engine.health.gather;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class StreamActor {

	public Object doAction(String path, Action action) {
		try {
			return this.doAction(new FileReader(path), action);
		} catch (FileNotFoundException e) {
			return null;
		}
	}

	public Object doAction(Reader reader, Action action) {
		try {
			return action.run(new BufferedReader(reader));
		} catch (IOException e) {
			return null;
		} finally {
			close(reader);
		}
	}

	private void close(Closeable cls) {
		try {
			if (cls != null) {
				cls.close();
			}
		} catch (IOException e) {
			cls = null;
		}
	}

	public interface Action {
		public Object run(BufferedReader reader) throws IOException;
	}

	public static void main(String[] args) throws Exception {
		StreamActor actor = new StreamActor();
		actor.doAction(new java.io.FileReader("/proc/loadavg"), new Action() {
			public Object run(BufferedReader reader) {
				return new java.util.Date();
			}
		});
	}
}
